package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.BigDataConfig;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.exception.BigDataException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.http.CommonResponse;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.system.domain.request.historyvideo.HistoryVideoDeviceCloseParam;
import com.ruoyi.system.domain.request.historyvideo.HistoryVideoDeviceListParam;
import com.ruoyi.system.domain.vo.realtimevideo.DeviceVideo;
import com.ruoyi.system.service.OrderSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述
 * 作者 ZhangPing
 * 创建时间 2023/10/30 15:11
 */
@Service
public class OrderSendServiceImpl implements OrderSendService {

    @Autowired
    private BigDataConfig bigDataConfig;

    /**
     * 查询设备历史媒体资源列表
     */
    @Override
    public List<DeviceVideo> queryMediaList(HistoryVideoDeviceListParam param){
        JSONObjectPro orderContent = new JSONObjectPro()
                .put("beginTime", param.getStartTime())
                .put("endTime", param.getEndTime())
                .put("devNo", param.getDvo())
                .put("chanelId", param.getChannel())
                .put("response", true)
                .put("requestId", SnowFlakeIDGenerator.getInstance().nextId())
                .put("distributeTime", DateUtils.getTime())
                .put("distributeMsgTopic", "QueryMediaList_Send");
        JSONObjectPro orderParam = new JSONObjectPro()
                .put("id", SnowFlakeIDGenerator.getInstance().nextId())
                .put("content", JSONObject.toJSONString(orderContent));
        CommonResponse<JSONObject> response = HttpRequest.postJson(bigDataConfig.getGatewayApiUrl() + "/KafkaApiController/queryMediaList", orderParam, JSONObject.class);
        if (!response.isSuccess()) throw  new BigDataException();
        JSONObject resultJson = response.getData().getJSONObject("result");
        return resultJson.getList("mediaListItems", DeviceVideo.class);
    }

    /**
     * 开启设备历史视频
     */
    @Override
    public void openDeviceVideo(HistoryVideoDeviceListParam param){
        JSONObjectPro orderContent = new JSONObjectPro()
                .put("ip", bigDataConfig.getDeviceVideoPlayIp())
                .put("tcpPort", bigDataConfig.getDeviceVideoPlayPort())
                .put("codeType", param.getCodeType())
                .put("storageType", param.getStorageType())
                .put("mediaType", param.getMediaType())
                .put("beginTime", param.getStartTime())
                .put("endTime", param.getEndTime())
                .put("devNo", param.getDvo())
                .put("chanelId", param.getChannel())
                .put("requestId", SnowFlakeIDGenerator.getInstance().nextId())
                .put("distributeTime", DateUtils.getTime())
                .put("distributeMsgTopic", "HisMediaUploadReq_Send");
        JSONObjectPro orderParam = new JSONObjectPro()
                .put("id", SnowFlakeIDGenerator.getInstance().nextId())
                .put("content", JSONObject.toJSONString(orderContent));
        CommonResponse<Object> response = HttpRequest.postJson(bigDataConfig.getGatewayApiUrl() + "/KafkaApiController/distributeAll", orderParam, Object.class);
        if (!response.isSuccess()) throw  new BigDataException();
    }

    /**
     * 关闭设备历史视频
     */
    @Override
    public void closeDeviceVideo(HistoryVideoDeviceCloseParam param){
        JSONObjectPro orderContent = new JSONObjectPro()
                .put("playControl", 2)
                .put("playStartTime", DateUtils.getTime())
                .put("devNo", param.getDvo())
                .put("chanelId", param.getChannel())
                .put("requestId", SnowFlakeIDGenerator.getInstance().nextId())
                .put("distributeTime", DateUtils.getTime())
                .put("distributeMsgTopic", "HisMediaUploadCmd_Send");
        JSONObjectPro orderParam = new JSONObjectPro()
                .put("id", SnowFlakeIDGenerator.getInstance().nextId())
                .put("content", JSONObject.toJSONString(orderContent));
        CommonResponse<Object> response = HttpRequest.postJson(bigDataConfig.getGatewayApiUrl() + "/KafkaApiController/distributeAll", orderParam, Object.class);
        if (!response.isSuccess()) throw  new BigDataException();
    }
}
